import math

class PenmanMonteith:
	"""
	蒸散量模型计算
	"""
	
	Tmax = 35.0 # 最高气温
	Tmin = 19.0 # 最低气温
	RHmean = 30.0 # 相对湿度
	WindSpeed = 2.5 # 风速 m/s
	SunshineDuration = 9.5 # 日照时数(n)
	Elevation = 1010.0 # 海拔(z)
	kc = 0.65 # 棉花吐絮期
	pe = 0.0 # 有效降水量Pe
	IrrigationDepth = 0.7 # 浇灌深度
	FieldCapacity = 5.0 # 田间持水量
	BulkDensity = 1.6 # 土壤容重
	SoilMoistureContent = 2.0 # 土壤含水量
	SprinklerIrrigation = 0.7 # 灌溉方式喷灌
	LandArea = 3.0 # 土地面积  亩

	def __init__(self):
		pass
		
	def setDate(self, date):
		"""
		需计算日期
		"""
		self.date = date
		
	def setWeather(self):
		"""
		气象站数据
		"""
		
		self.Tmax = 35.0 # 最高气温
		self.Tmin = 19.0 # 最低气温
		self.RHmean = 30.0 # 相对湿度
		self.WindSpeed = 2.5 # 风速 m/s
		self.SunshineDuration = 9.5 # 日照时数(n)
	
	def setGeography(self):
		"""
		地理位置
		"""
		self.Latitude = "40°30′N" # 纬度(φ)
		self.Elevation = 1010.0 # 海拔(z)
	
	def calculate(self):
		"""
		数据计算
		"""
	
		# 平均气温T
		T = (self.Tmax + self.Tmin) / 2
		print u"平均气温T:", T
		
		# 饱和水汽压es
		es = (0.6108*math.exp(17.27* self.Tmax /(self.Tmax +237.3)) + 0.6108 * math.exp(17.27* self.Tmin /(self.Tmin + 237.3))) / 2
		print u"饱和水汽压es:", es
		
		# 实际水汽压ea
		ea = es * self.RHmean / 100
		print u"实际水汽压ea:", ea
		
		# 饱和水汽压曲线斜率Δ
		Delta = 4098 * es / ((T+237.3)*(T+237.3))
		print u"饱和水汽压曲线斜率Δ:", Delta
		
		# 净辐射Rn
		Rn = 26.34
		
		# 土壤热通量G
		G = 0.1 * Rn
		
		# 海拔修正P
		P = 101.3 * pow((293-0.0065 * self.Elevation) / 293, 5.26)
		print u"海拔修正P", P
		
		# 湿度计常数γ
		PsychrometricConstant = (1.013 * 0.001 * P)/(2.45 * 0.622)
		print u"湿度计常数γ", PsychrometricConstant
		
		# 参考作物蒸散量ETo
		eto = (0.408*Delta*(Rn-G)+PsychrometricConstant*900/(T+273)*self.WindSpeed*(es-ea))/(Delta+PsychrometricConstant*(1+0.34*self.WindSpeed))
		print u"参考作物蒸散量ETo", eto
		
		IrrigationVolume = (eto * self.kc - self.pe + (self.IrrigationDepth * (self.FieldCapacity - self.SoilMoistureContent) * self.BulkDensity)) / self.SprinklerIrrigation
		print u"单位面积灌水量", IrrigationVolume
		
		DayIrrigationVolume = IrrigationVolume * self.LandArea * 666.67 / 1000
		print u"三亩地日需灌溉量", DayIrrigationVolume
		
		return self
		
		
class Fertilization:
	"""
	追肥量模型计算
	"""
	
	NDVI = 0.4
	
	Stages = [u"盛蕾期", u"花期", u"盛玲期", u"初絮期"]
	NDVIS = [0.695, 0.833, 0.881, 0.809]
	NitrogenFertilizer = [0.016, 0.061, 0.074, 0.011]
	
	def __init__(self):
		
		index = min(range(len(self.NDVIS)), key=lambda i: abs(self.NDVIS[i] - self.NDVI))
		
		NDVIValue = self.NDVIS[index]
		for index, value in enumerate(self.Stages):
			print (NDVIValue - self.NDVI) * self.NitrogenFertilizer[index]
	
	
		return self
	